package 华为OD机试B卷复用题.基础题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class HJ22_判断字符串子序列 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试B卷复用题/基础题/input/22.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    // 参考 HJ15，两者思路几乎一样，一个正序遍历，一个逆序遍历。
    static void Solution(Scanner sc) {
        String target = sc.nextLine();
        String source = sc.nextLine();

        int targetIndex = target.length() - 1;
        int sourceIndex = source.length() - 1;

        while (targetIndex >= 0 && sourceIndex >= 0) {
            if (target.charAt(targetIndex) == source.charAt(sourceIndex)) {
                targetIndex--;
            }
            sourceIndex--;
        }

        if (targetIndex == -1) {
            System.out.println(sourceIndex + 1);
        } else {
            System.out.println(-1);
        }
    }
}
